package logic.ui.componentInfoMed
{
	import com.adobe.utils.ArrayUtil;
	
	import common.ext.font.TextFormatLib;
	import common.manager.GUI;
	import common.manager.ObserverEvt;
	
	import component.show.control.UISprite;
	import component.untils.ComponentItem;
	
	import fl.controls.*;
	import fl.data.DataProvider;
	import fl.events.ComponentEvent;
	
	import flash.events.Event;
	import flash.text.TextFormat;
	
	import logic.def.Def;
	import logic.def.UIURL;
	import logic.manager.XMLManager;

	public class ButtonInfoMed extends UISprite
	{
		public static const NAME : String = "Button";
		
		private var m_target : ComponentItem;
		
		private var m_txtID : TextInput;
		private var m_txtX : TextInput;
		private var m_txtY : TextInput;
		private var m_txtWidth : TextInput;
		private var m_txtHeight : TextInput;
		
		private var m_comTextFormat : ComboBox;
		
		private var m_dpTextFormat : DataProvider;
		
		private var m_txtHtmlText : TextInput;
		
		public function ButtonInfoMed()
		{
			super(NAME,UIURL["Button"]);
			
			m_txtID = collection["txtId"];
			m_txtHtmlText = collection["txtHtmlText"];
			m_txtX = collection["Txt_X"];
			m_txtY = collection["Txt_Y"];
			m_txtWidth = collection["Txt_Width"];
			m_txtHeight = collection["Txt_Height"];
			m_comTextFormat = collection["ComTextFormat"];
			
			m_dpTextFormat = new DataProvider;
			
			m_comTextFormat.dataProvider = m_dpTextFormat;
			
			m_comTextFormat.labelField = "name";
			var lst : Array = ArrayUtil.objectToArray(XMLManager.TextFormat.data,"sort",Array.NUMERIC);
			lst.sortOn("sort",Array.NUMERIC);
			m_dpTextFormat.addItems(lst);
			
			m_txtID.addEventListener(Event.CHANGE,onIdChanged);
			m_txtHtmlText.addEventListener(Event.CHANGE,onHtmlTxtChanged);
			m_txtX.addEventListener(ComponentEvent.ENTER,setX);
			m_txtY.addEventListener(ComponentEvent.ENTER,setY);
			m_txtWidth.addEventListener(ComponentEvent.ENTER,setWidth);
			m_txtHeight.addEventListener(ComponentEvent.ENTER,setHeight);
			m_comTextFormat.addEventListener(Event.CHANGE,onComTextFormatExhanged);
		}
		
		public function set target(btn : ComponentItem) : void
		{
			m_target = btn;
			setTargetData();
			
			setComTextFormatPoint();
		}
		
		public function setTargetData() : void
		{
			var btn : Button = m_target.item;
			
			m_txtID.text = m_target.item.name;
			m_txtHtmlText.htmlText = btn.label;
			m_txtX.text = m_target.item.x.toString();
			m_txtY.text = m_target.item.y.toString();
			m_txtWidth.text = m_target.item.width.toString();
			m_txtHeight.text = m_target.item.height.toString();
			
			if(m_target.txtId)
			{
				m_txtHtmlText.htmlText = m_target.txtId;
			}
		}
		
		private function onIdChanged(evt : Event) : void
		{
			m_target.item.name = m_txtID.text;
			
			sendChangeMsg();
		}
		
		private function onHtmlTxtChanged(evt : Event) : void
		{
			if(!m_target) return;
			
			var str : String = m_txtHtmlText.text;
			
			if(XMLManager.Text.data[str] && !(XMLManager.Text.data[str] is Function))
			{
				m_target.item.htmlText = XMLManager.Text.data[str]["text"];
				m_target.txtId = str;
			}
			else
			{
				m_target.item.label = str;
				sendChangeMsg();
			}
		}
		
		private function setX(evt : ComponentEvent) : void
		{
			m_target.item.x = Number(m_txtX.text);
			
			sendChangeMsg();
		}
		
		private function setY(evt : ComponentEvent) : void
		{
			m_target.item.y = Number(m_txtY.text);
			
			sendChangeMsg();
		}
		
		private function setWidth(ev : ComponentEvent) : void
		{
			m_target.item.width = Number(m_txtWidth.text);
			
			sendChangeMsg();
		}
		
		private function setHeight(evt : ComponentEvent) : void
		{
			m_target.item.height = Number(m_txtHeight.text);
			
			sendChangeMsg();
		}
		
		private function sendChangeMsg() : void
		{
			ObserverEvt.Send(Def.COMPONENTPROPERTYCHANGED);
		}
		
		/**
		 * 设置文本格式
		 * @param evt 
		 */		
		private function onComTextFormatExhanged(evt : Event) : void
		{
			var cellData : Object = m_comTextFormat.selectedItem;
			var format:TextFormat = TextFormatLib[cellData["name"]];
			
			m_target.item.setStyle("textFormat",format);
			
			m_target.textFormatName = cellData["name"];
		}
		
		private function setComTextFormatPoint(value : TextFormat = null) : void
		{
			var lst : Array = m_comTextFormat.dataProvider.toArray();
			var length : int = lst.length;
			
			var curTextFormat : TextFormat;
			
			if(!value)
			{
				curTextFormat = m_target.item.getStyle("textFormat") as TextFormat;
			}
			else
			{
				curTextFormat = value;
			}
			
			for(var i:int=0;i<length;i++)
			{
				var textFormat : * = TextFormatLib[lst[i]["name"]];
				
				if(textFormat == curTextFormat)
				{
					m_comTextFormat.selectedItem = lst[i];
					break;
				}
			}
		}
	}
}